//index.js
//获取应用实例
var app = getApp()

Page({
  data: {
   loading:true,
   focus:false
  },
  onLoad: function () {
   var that = this;
   that.getData();
  },
  getData:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api+'/index/index',
        data:{
          t_session:session
        },
        success:function(res){
          var res = res.data;
          console.log(res);
          if(res.done){
            var data = res.retval
            that.setData({
              list:data.list,
              loading:false
            })
          }else{
            that.setData({
              loading:true,
              errorTips:'接口请求错误'
            })
          }
        },
        fail:function(res){
          that.setData({
            loading: true,
            errorTips: '接口请求错误'
          })
        }
      })
  })
  },
  //获得焦点
  focus:function(res){
    this.setData({
      focus:this.data.focus?false:true
    })
  },
  //获得用户输入
  input:function(res){
    var value = res.detail.value;
    this.setData({
      value:value
    })
  },
  //提交
  submit:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/index/search',
        data:{
          keywords:that.data.value,
          t_session:session
        },
        success:function(res){
          var res =res.data;
          if(res.done&&res.retval.list.length>0){
            wx.navigateTo({
              url: '/pages/search_result/search_result?keywords='+that.data.value
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '暂无相关内容',
            })
          }
        },fail:function(res){
          wx.showToast({
            title: '失败，请重试！',
            icon:'loading'
          })
        }
      })
  })
  }
})
